package com.leetcode.LC;

import java.util.ArrayList;
import java.util.TreeSet;

public class LC720 {
    public String longestWord(String[] words) {

        TreeSet<String> set = new TreeSet<>();
        for (int i = 0; i < words.length; i++) {
            set.add(words[i]);
        }
        int i = -1;
        for (String s : set) {
            System.out.print(s + ",");
            i++;
            words[i] = s;
        }
        System.out.println();

        ArrayList<String> list = new ArrayList<>();
        int j;
        for (j = 0; j < words.length - 1; j++) {
            if (words[j + 1].contains(words[j]) && (j + 1 == words.length - 1)) {
                list.add(words[j + 1]);
            } else if (words[j + 1].contains(words[j])) {
                continue;
            } else {
                list.add(words[j]);
            }
        }
        int max = 0;
        String rs = "";
        for (String s : list) {
            System.out.print(s + ",");
            if (s.length() > max) {
                max = s.length();
                rs = s;
            }
        }
        System.out.println();
        return rs;

    }

    public static void main(String[] args) {
        System.out.println(new LC720().longestWord(new String[]{"yo","ew","fc","zrc","yodn","fcm","qm","qmo","fcmz","z","ewq","yod","ewqz","y"}));
    }
}
